#include "PointIni.h"

PointIni::PointIni()
{

}

bool PointIni::parser()
{
    if(!this->check())
    {
        return false;
    }

    QSettings settings(m_path, QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    int zoneNum = settings.value("zone.num_i").toInt();
    for(int i=0; i<zoneNum; i++)
    {
        QString keyZone = "zone." + QString::number(i,10) + ".name";
        QString zoneName = settings.value(keyZone).toString();
        m_zoneHash.insert(i,zoneName);
    }

    int pointNum = settings.value("controlPoint.num_i").toInt();
    for(int i=0; i<pointNum; i++)
    {
        PointStruct point;

        point.setIndex(i);
        point.setValueType(settings.value("controlPoint." + QString::number(i,10) + ".valueType_i").toInt());
        point.setAddress(settings.value("controlPoint." + QString::number(i,10) + ".address_i").toInt());
        point.setDefaultValue(settings.value("controlPoint." + QString::number(i,10) + ".defaultValue_i").toInt());
        point.setName(settings.value("controlPoint." + QString::number(i,10) + ".name").toString());
        point.setZoneName(settings.value("controlPoint." + QString::number(i,10) + ".zoneName").toString());
        point.setDeviceType(settings.value("controlPoint." + QString::number(i,10) + ".deviceType").toString());
        point.setFuncType(settings.value("controlPoint." + QString::number(i,10) + ".funType").toString());
        point.setDeviceIndex(settings.value("controlPoint." + QString::number(i,10) + ".deviceIndex_i").toInt());


        if (point.valueType() == 2)
        {
            point.setMax(settings.value("controlPoint." + QString::number(i,10) + ".maxValue_i").toInt());
            point.setMin(settings.value("controlPoint." + QString::number(i,10) + ".minValue_i").toInt());

        }
        else if (point.valueType() == 3)
        {
            QStringList strList = settings.value("controlPoint." + QString::number(i,10) + ".intList_ia").toStringList();
            QList<int> intList;
            for(int j=0; j<strList.size(); j++)
            {
                intList.append(strList.at(j).toInt());
            }
            point.setIntList(intList);
        }

        QStringList valueList = settings.allKeys().filter("controlPoint." + QString::number(i,10) + ".value.");
        QMultiHash<QString,int> valueTable;
        for(int j=0; j<valueList.size(); j++)
        {
            QString valueKey = valueList.at(j);
            int frontIndex,behindIndex;
            frontIndex  = valueKey.indexOf(".");
            frontIndex  = valueKey.indexOf(".",frontIndex+1);
            frontIndex  = valueKey.indexOf(".",frontIndex+1);
            behindIndex = valueKey.indexOf(".",frontIndex+1);

            int value = valueKey.mid(frontIndex+1,behindIndex-frontIndex-1).toInt();
            QString valueName = settings.value(valueKey).toString();
            valueTable.insert(valueName,value);
        }
        point.setValueTable(valueTable);

        m_pointHash.insert(point.zoneName(),point);
    }

    return true;
}



void PointIni::clear()
{

}
QMultiHash<QString, PointStruct> PointIni::pointHash() const
{
    return m_pointHash;
}

QHash<int, QString> PointIni::zoneHash() const
{
    return m_zoneHash;
}



